package com.tts.superstar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tts.superstar.Database.AccountDatabase;
import com.tts.superstar.View.BaseActivity;

public class LoginActivity extends BaseActivity {
    EditText editText1,editText2;
    TextView textView1,textView2;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        editText1 = findViewById(R.id.edit_account_login);
        editText2 = findViewById(R.id.edit_password_login);
        textView1 = findViewById(R.id.text_zhuce_login);
        textView2 = findViewById(R.id.text_wangjimima_login);
        button = findViewById(R.id.btn_denglu);
        initLisn();
    }

    private void initLisn() {


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String s1 = editText1.getText().toString();
                String s2 = editText2.getText().toString();
                AccountDatabase database = new AccountDatabase(LoginActivity.this);
                SQLiteDatabase db = database.getReadableDatabase();
                Cursor cursor = db.rawQuery("select * from account where zhanghao = ? and mima = ?",new String[]{s1,s2});
                if(cursor.getCount() == 1){
                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                    startActivity(intent);
                    SharedPreferences sharedPreferences = getSharedPreferences("account",MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putBoolean("isLogin",true);
                    editor.putString("User",s1);
                    editor.apply();
                    finish();
                }else {
                    Toast.makeText(LoginActivity.this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                }

            }
        });
        textView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(LoginActivity.this,RegActivity.class);
                startActivity(intent);
            }
        });
    }

}
